package service

import (
	"encoding/json"
	"fmt"
	"nt-order/common"
	"nt-order/conf"
	"strings"
)

type StaffIdFromPermissionCenter struct {
	Token string
}

func (that *StaffIdFromPermissionCenter) UserStaffid() string {
	url := strings.TrimSpace(conf.Env.Url.PermissionCenterURL + "/rest/auth/user/staffid")
	bytes, err := common.Curl("get", url, nil, map[string]string{
		"Authorization": that.Token,
	})

	if err != nil {
		panic(err)
	}

	type Data struct {
		StaffId string `json:"staffId"`
	}
	type Result struct {
		Code int    `json:"code"`
		Msg  string `json:"msg"`
		Data Data   `json:"data"`
	}

	result := Result{}
	err = json.Unmarshal(bytes, &result)
	if err != nil {
		panic(err)
	}

	if result.Code != 0 {
		panic(fmt.Errorf("token:%s,code:%d,msg:%s", that.Token, result.Code, result.Msg))
	}

	return result.Data.StaffId
}
